package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.NewsDetailsDaoImpl;
import com.ysd.entity.NewsClassify;
import com.ysd.entity.NewsDetails;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

/**
 * 按条件查询新闻资讯
 * localhost:8080/shangcheng1/adminNews?reqName=getSelNews
 * @author Administrator
 *
 */
public class AdminNewsServlet extends BaseServlet {
	
	public void getSelNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//接受前台传来的参数
		int newsDetailsId = 0;//资讯id非空判定
		if(request.getParameter("newsDetailsId")!=null && !request.getParameter("newsDetailsId").equals("")) {
			newsDetailsId = Integer.parseInt(request.getParameter("newsDetailsId"));
		}
		
		int newsClassifyId = 0;//资讯所属分类
		if(request.getParameter("newsClassifyId")!=null && !request.getParameter("newsClassifyId").equals("")) {
			newsClassifyId = Integer.parseInt(request.getParameter("newsClassifyId"));
		}
		
		String title = request.getParameter("title");//标题
		String introduction = request.getParameter("introduction");//推荐语
		String content = request.getParameter("content");//内容
		
		NewsDetails n = new NewsDetails(newsDetailsId, title, introduction, content, newsClassifyId, "");
		
		NewsDetailsDaoImpl newDao = new NewsDetailsDaoImpl();
		
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		List<NewsDetails> newList = newDao.getNewsByPage(n, page, size);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		Gson gson = new Gson();
		String res = gson.toJson(newList);
		out.print(res);
		
		out.flush();
		out.close();
		
	}

	/**
	 * localhost:8080/shangcheng1/adminNews?reqName=getSelNewsByPage
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getSelNewsByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//接受前台传来的参数
		int newsDetailsId = 0;//资讯id非空判定
		if(request.getParameter("newsDetailsId")!=null && !request.getParameter("newsDetailsId").equals("")) {
			newsDetailsId = Integer.parseInt(request.getParameter("newsDetailsId"));
		}
		
		int newsClassifyId = 0;//资讯所属分类
		if(request.getParameter("newsClassifyId")!=null && !request.getParameter("newsClassifyId").equals("")) {
			newsClassifyId = Integer.parseInt(request.getParameter("newsClassifyId"));
		}
		
		String title = request.getParameter("title");//标题
		String introduction = request.getParameter("introduction");//推荐语
		String content = request.getParameter("content");//内容
		
		NewsDetails n = new NewsDetails(newsDetailsId, title, introduction, content, newsClassifyId, "");
		
		NewsDetailsDaoImpl newDao = new NewsDetailsDaoImpl();
		
		
		List<NewsDetails> newList = newDao.getNewsByWhere(n);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		Gson gson = new Gson();
		String res = gson.toJson(newList.size());
		out.print(res);
		
		out.flush();
		out.close();
		
	}
	
	/**
	 * 获取产品的所有详情
	 * @param localhost:8080/shangcheng1/adminNews?reqName=delAdminNewsDetails
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delAdminNewsDetails(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		int newsId = Integer.parseInt(request.getParameter("newsDetailsId"));
		NewsDetailsDaoImpl ndDao = new NewsDetailsDaoImpl();//创建对象
		int f = ndDao.delNewsDetails(newsId);//调用方法
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();//创建out对象
		
		if (f > 0) {//删除成功
			out.print(Result.toClient(true, "删除成功", ""));
		} else {//删除失败
			out.print(Result.toClient(false, "删除失败", ""));
		}
		
		out.flush();
		out.close();
		
	}

	/**
	 * 获取产品的所有详情
	 * @param localhost:8080/shangcheng1/adminNews?reqName=addNewsDetail
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addNewsDetail(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		NewsDetailsDaoImpl nDao = new NewsDetailsDaoImpl();
		
		String title = request.getParameter("title");
		String introduction = request.getParameter("introduction");
		String content = request.getParameter("content");
		int newsClassifyId = 0;
		if(request.getParameter("newsClassifyId")!=null && !request.getParameter("newsClassifyId").equals("")) {
			newsClassifyId = Integer.parseInt(request.getParameter("newsClassifyId"));
		}
		if(title!=null && !title.equals("")) {
			List<NewsDetails> clList = nDao.getProDetailsByTitle(title);
			System.out.println(clList);
			if (clList.size()==0) {
				NewsDetails ne = new NewsDetails(0, title, introduction, content, newsClassifyId);
				System.out.println(ne);
				
				int n = nDao.addNewDetails(ne);
				
				if(n > 0) {
					out.print(Result.toClient(true, "添加成功", ""));
				} else {
					out.print(Result.toClient(false, "添加失败", ""));
				}
			} else {
				out.print(Result.toClient(false, "标题已存在", ""));
			}
		}
		
		out.flush();
		out.close();
		
	}

	/**
	 * @param localhost:8080/shangcheng1/adminNews?reqName=addNewsDetail
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getNewsDetailsById(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		NewsDetailsDaoImpl nDao = new NewsDetailsDaoImpl();
		
		int newId = Integer.parseInt(request.getParameter("newsDetailsId"));
		NewsDetails nd = nDao.selNewsDetailsById(newId);
		
		Gson gson = new Gson();
		String res = gson.toJson(nd);
		out.print(res);
		
		
		out.flush();
		out.close();
		
	}
	
	/**
	 * @param localhost:8080/shangcheng1/adminNews?reqName=updateNewsDetails
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateNewsDetails(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		NewsDetailsDaoImpl nDao = new NewsDetailsDaoImpl();
		
		int newsDetailsId = Integer.parseInt(request.getParameter("newsDetailsId"));
		String title = request.getParameter("title");
		String introduction = request.getParameter("introduction");
		String content = request.getParameter("content");
		int newsClassifyId = Integer.parseInt(request.getParameter("newsClassifyId"));
		
		if(title!=null && !title.equals("")) {
			List<NewsDetails> neList = nDao.getProDetailsByTitle(title);
			System.out.println(neList);
			if (neList.size()==0) {
				NewsDetails news = new NewsDetails(newsDetailsId, title, introduction, content, newsClassifyId);
				System.out.println(news);
				int n = nDao.updateNewDetailsById(news);
				
				if(n > 0) {
					out.print(Result.toClient(true, "修改成功", ""));
				} else {
					out.print(Result.toClient(false, "修改失败", ""));
				}
			} else {
				out.print(Result.toClient(false, "标题存在", ""));
			}
		}
		
		out.flush();
		out.close();
		
	}
	
	/**
	 * @param localhost:8080/shangcheng1/adminNews?reqName=delNewsDetailsByIds
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delNewsDetailsByIds(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		NewsDetailsDaoImpl nDao = new NewsDetailsDaoImpl();
		
		String ids = request.getParameter("ids");
		int n = nDao.delNewsDetailsByIds(ids);
				
			if(n > 0) {
				out.print(Result.toClient(true, "修改成功", ""));
			} else {
				out.print(Result.toClient(false, "修改失败", ""));
			}
			
			out.flush();
			out.close();
			
	}
	
}
